#pragma once

#include <ctemplate/template.h>
#include <vector>
#include "oj_model.h"

// view:构建网页,渲染网页内容(前段内容)
namespace ns_view
{
    using namespace ns_model;
    using namespace ns_log;
    using namespace ns_util;
    using namespace ns_model;

    const string template_path = "./template_html/";

    class View
    {
    public:
        // 根据题目信息渲染html文件并返回
        bool OneStructure(const Question &one, string *html)
        {
            string target_html = template_path + "OneQuestion.html";

            // 1.形成数据字典
            ctemplate::TemplateDictionary root("mp"); // unorder_map<> mp
            root.SetValue("nub", one._nub);
            root.SetValue("name", one._name);
            root.SetValue("level", one._level);
            root.SetValue("header", one._header);
            root.SetValue("desc", one._desc);
            root.SetValue("cpu_limit", to_string(one._cpu_limit));
            root.SetValue("mem_limit", to_string(one._mem_limit));

             // 2.获取指定html的渲染对象
            ctemplate::Template *tp = ctemplate::Template::GetTemplate(target_html, ctemplate::DO_NOT_STRIP);

            // 3.替换字典数据到网页，同时返回给html串
            tp->Expand(html, &root); // 渲染html网页中的key字段

            return true;
        }
        bool AllStructure(const vector<Question> &all, string *html)
        {
            string target_html = template_path + "AllQuestions.html";

            // 1.形成数据字典
            ctemplate::TemplateDictionary root("mp"); // unorder_map<> mp
            for (auto &q : all)
            {
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("question_list");
                sub->SetValue("nub", q._nub);
                sub->SetValue("name", q._name);
                sub->SetValue("level", q._level);
            }

            // 2.获取指定html的渲染对象
            ctemplate::Template *tp = ctemplate::Template::GetTemplate(target_html, ctemplate::DO_NOT_STRIP);

            // 3.替换字典数据到网页，同时返回给html串
            tp->Expand(html, &root); // 渲染html网页中的key字段

            return true;
        }

    private:
    };
};
